#include "sagevistextlabel.h"

SageVisTextLabel::SageVisTextLabel(QPointer<SageVis> sageVisPointer, QPointer<VisBaseWidget> parentWidget, QGraphicsItem *parent ) :
    VisBaseElement( sageVisPointer, parentWidget, parent)
{
    init("");//setup text label with default empty text
}


SageVisTextLabel::SageVisTextLabel(QString t, QPointer<SageVis> sageVisPointer, QPointer<VisBaseWidget> parentWidget, QGraphicsItem *parent ) :
    VisBaseElement( sageVisPointer, parentWidget, parent)
{
    init(t);//setup text label with text param
}

void SageVisTextLabel::init( QString t )
{
    textString = t;

    setAcceptsHoverEvents(true);//when hovering over a label, make it more prominent
    hovering = false;//currently not hovering

    setupText();
}


void SageVisTextLabel::setText(QString t )
{
    textString = t;
    setupText();
}

int SageVisTextLabel::getTextWidth()
{
//    QPainter painter;
//    painter.setFont(*font);
//    QFontMetrics fm = painter.fontMetrics();//font metrics allows us to compute text width
//    textWidth = fm.width(text->toPlainText());//gives calculated text width with current QFont in QPainter

    return textWidth;
}

void SageVisTextLabel::setLabelWidth(int labelWidth)
{
    this->labelWidth = labelWidth;
}

void SageVisTextLabel::setupText()
{
    //SETUP TEXT, FONT, TEXT SIZE, Z VALUE

    font = new QFont( "Helvetica [Cronyx]", 12);
    font->setPixelSize(13);//setPointSize( 13 );//for now
    textColor = new QColor( 200.0, 200.0, 200.0, 255.0 );//white colored text
//    text->setFont( *font );
//    text->setDefaultTextColor ( *textColor );
//    text->setZValue( 1000.0 );//make sure text drawn above other elts

    //init text width- at large text size.
    QPainter painter;
    painter.setFont(*font);
    QFontMetrics fm = painter.fontMetrics();//font metrics allows us to compute text width
    textWidth = fm.width(textString);//gives calculated text width with current QFont in QPainter

    //set geometry, so that line connection will work
    setGeometry( this->geometry().x(), this->geometry().y(), textWidth*1.05, this->geometry().height() );

    //SETUP LABEL BACKGROUND
//    behindTextRect = new QGraphicsRectItem(this);
//    behindTextRect->setRect(  boundingRect().x(), boundingRect().y(), textWidth, boundingRect().height() );
//    QBrush b;
//    QPen p;
//    p.setStyle( Qt::NoPen );
//    b.setColor(*backgroundColor );
//    b.setStyle( Qt::SolidPattern);
//    behindTextRect->setBrush(b);
//    behindTextRect->setPen(p);
//    behindTextRect->setZValue(900.0);
}

void SageVisTextLabel::setGeometry(const QRectF &geom)
{    
    super::setGeometry(geom);

    //no need to set pos.  rel position in parent remains the same
    //text->setPos(boundingRect().x(), boundingRect().y()-boundingRect().height()/2.0);//figuring out where to put text in bounding rect

    //no need to reset height.  this is done in paint
//    //reset label height
//    int textSize = (int) geom.height(); //- 2; //current font size
//    if( textSize%2 != 0 )
//        textSize--;
//    if( textSize > 12 )
//        textSize = 12;
//    if( textSize < 2 )
//        textSize = 2;

//    font->setPixelSize(textSize);
//    text->setFont( *font );
}

void SageVisTextLabel::setGeometry(qreal x, qreal y, qreal w, qreal h)
{
    setGeometry(QRectF( x, y, w, h ));
}

//reimplement
void SageVisTextLabel::processHoverEvent(QGraphicsSceneHoverEvent *event)
{
//    int textSize;
//    if( event->type() == QEvent::HoverEnter )//!hovering ) //if not hovering now, will be once super called
//    {
//        savedFontSize = (int) geometry().height();
//        //textSize = 12; //max text size
//        qDebug() << "hovering " << endl;
//        textColor->setRgb(255.0, 255.0, 255.0, 255.0);
//    }
//    else if( event->type() == QEvent::HoverLeave ) //is hovering now, will stop hovering once super called
//    {
////        textSize = savedFontSize;//(int) geometry().height();
////        if( textSize%2 != 0 )
////            textSize--;
////        if( textSize > 12 )
////            textSize = 12;
////        if( textSize < 2 )
////            textSize = 2;
//        qDebug() << "not hovering " << endl;
//        textColor->setRgb(200.0, 200.0, 200.0, 255.0);
//    }

//    qDebug() << "process hover signal in text label. " << text->toPlainText() <<  " size = " << textSize << endl;

//    //font->setPixelSize(textSize);
//    text->setFont( *font );
//    text->setDefaultTextColor ( *textColor );

//    super::processHoverEvent(event);
//    emit( recomputeLayoutSignal() ); //with height change, must recompute layout


    float newHeight = geometry().height();
    if( !hovering )//not hovering now, will soon
    {
        newHeight *= 3.0;//increase size if hovering
    }
    else
    {
        newHeight /= 3.0;//decrease size if no longer hovering
    }

    setHeight( newHeight );//set the height

    qDebug() << "contig processes hover event" << endl;

    super::processHoverEvent(event); //call base class to ensure hover event goes through

//    //weird bug...  on text setup, must place text at -1/2height
//    //on maximize, must shift back by +1.2 height
//    if( hovering )
//    {
//        text->setPos(boundingRect().x(), boundingRect().y()+boundingRect().height()/2.0);//figuring out where to put text in bounding rect
//    }
//    else
//    {
//        text->setPos(boundingRect().x(), boundingRect().y()-boundingRect().height()/2.0);//figuring out where to put text in bounding rect
//    }

    emit( recomputeLayoutSignal() ); //with height change, must recompute layout
}

void SageVisTextLabel::receiveHoverEvent(QGraphicsSceneHoverEvent *event)
{
    qDebug() << "receive hover signal in text label. " << textString << endl;

    super::processHoverEvent(event);

//    text->setPos(boundingRect().x(), boundingRect().y()-boundingRect().height()/2.0);//figuring out where to put text in bounding rect

//    //weird bug...  on text setup, must place text at -1/2height
//    //on maximize, must shift back by +1.2 height
//    if( hovering )
//    {
//        text->setPos(boundingRect().x(), boundingRect().y()+boundingRect().height()/2.0);//figuring out where to put text in bounding rect
//    }
//    else
//    {
//        text->setPos(boundingRect().x(), boundingRect().y()-boundingRect().height()/2.0);//figuring out where to put text in bounding rect
//    }

    emit( recomputeLayoutSignal() ); //with height change, must recompute layout

}



void SageVisTextLabel::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,
           QWidget *widget)
{
    //get updated label height
        //note: not in change height because of layout bug
    int textSize = (int) geometry().height(); //- 2; //current font size
//    if( textSize%2 != 0 )
//        textSize--;
    //if( textSize > 12 )
    //    textSize = 12;
    //if( textSize < 2 )
    //    textSize = 2;

    font->setPixelSize(textSize);

    //must recompute text width with active painter
    painter->setFont(*font);
    QFontMetrics fm = painter->fontMetrics();//font metrics allows us to compute text width
    int newTextWidth = fm.width(textString);//gives calculated text width with current QFont in QPainter

    //reset geometry if necessary
    if( newTextWidth != textWidth )
    {
        textWidth = newTextWidth;
        setGeometry( this->geometry().x(), this->geometry().y(), textWidth*1.05, this->geometry().height() );
//        text->setGeometry( this->geometry().x(), this->geometry().y(), textWidth, this->geometry().height() );

    }

    QColor c;
    c.setRgb(255.0, 255.0, 255.0);
    painter->setPen(c);
    painter->drawText(boundingRect(), textString);

    //FOR DEBUGGING
//    QColor c;
//    c.setRgb(255.0, 0.0, 0.0);
//    painter->setPen(c);
//    painter->drawRect(boundingRect().x(), boundingRect().y(), textWidth*1.05, boundingRect().height());

//    QColor c2;
//    c2.setRgb(0.0, 255.0, 0.0);
//    painter->setPen(c2);
//    painter->drawRect(text->boundingRect().x(), text->boundingRect().y(), text->boundingRect().width(), text->boundingRect().height());


//    if( text->boundingRect().height() == boundingRect().height() )
//    {
//        QColor c2;
//        c2.setRgb(255.0, 0.0, 0.0);
//        painter->setPen(c);
//        painter->drawRect(boundingRect().x(), boundingRect().y(), textWidth*1.05, boundingRect().height());
//    }

//    QPointF textPos = text->pos();
//    QColor tc;
//    tc.setRgb( 0.0, 0.0, 255.0);
//    painter->setBrush(tc);
//    painter->setPen(tc);
//    painter->drawEllipse(textPos, 1.0, 1.0);
}


//QRectF SageVisTextLabel::boundingRect() const
//{
//    return QRectF(QPointF(0,0), geometry().size());
//}


//QPainterPath SageVisTextLabel::shape() const
//{
//    QPainterPath path;
//    path.addRect(boundingRect());
//    return path;
//}


//QSizeF SageVisTextLabel::sizeHint(Qt::SizeHint which, const QSizeF &constraint) const
//{
//    switch (which) {
//    case Qt::MinimumSize:
//        return QSizeF(500, 10);
//    case Qt::PreferredSize:
//        return QSizeF(500, 20);
//    case Qt::MaximumSize:
//        return QSizeF(500, 30);
//    default:
//        break;
//    }
//    return constraint;
//}
